// pages/creat/tags.js

const common = require('../../mixins/forum')

const options = {

  /**
   * 页面的初始数据
   */
  data: {
    tagpage: 1,
    tags: [],
    userSelectedTags: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var a = JSON.parse(options.tags);
    "" != a && this.setData({
      userSelectedTags: a
    });
    this.getTagList({
      per_page:20,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  addtag: function (e) {
    this.setData({
      searchStr: e.detail.value,
    })
  },

  /**
   * 添加标签 
   * */
  searchTag: function (e) {
    console.log(e);
    if (this.data.userSelectedTags.length > 8) {
      wx.showToast({
        title: "最多只能添加8个标签",
        image: "../../image/icon_warning.png",
        duration: 2e3
      })
      return
    }
    if (this.data.searchStr) {
      e.detail.value = this.data.searchStr
    }
    if (e.detail.value > 10) {
      wx.showToast({
        title: "标签应小于10个字",
        image: "../../image/icon_warning.png",
        duration: 2e3
      })
      return
    }
    let readsave = true
    // 过滤重复值
    if (this.data.userSelectedTags.length > 0) {
      for (let i = 0; i < this.data.userSelectedTags.length; i++) {
        if (this.data.userSelectedTags[i] == e.detail.value) {
          readsave = false
          break;
        }
      }

    }
    if (readsave) {
      this.data.userSelectedTags.unshift(e.detail.value);
      this.setData({
        searchInput: '',
        userSelectedTags: this.data.userSelectedTags
      })
    }



  },


  addTags: function (e) {
    let tags = e.currentTarget.dataset.name
    let readsave = true
    // 过滤重复值
    if (this.data.userSelectedTags.length > 0) {
      for (let i = 0; i < this.data.userSelectedTags.length; i++) {
        if (this.data.userSelectedTags[i] == tags) {
          readsave = false
          wx.showToast({
            title: '已添加',
          })
          break;
        }
      }

    }
    if (readsave) {
      this.data.userSelectedTags.unshift(tags);
      this.setData({
        userSelectedTags: this.data.userSelectedTags
      })
    }
  },



  /**
   * 保存
   */
  save: function () {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      tags: this.data.userSelectedTags,
    });
    wx.navigateBack({
      delta: 1
    })
  },


  /**
   * 删除标签
   */
  removeTag: function (e) {
    var a = e.currentTarget.dataset.tagindex;
    this.data.userSelectedTags.splice(a, 1), this.setData({
      userSelectedTags: this.data.userSelectedTags
    });
  },

  /**清除搜索 */
  activityClear: function () {
    this.setData({
      searchStr: '',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isBottom: true
    })
    if (!this.data.isLastPage) {
      this.getTagList({
        per_page:20,
        page: this.data.tagpage
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}
common(options);
Page(options);